dnl === configure.ac --------------------------------------------------------===
dnl SECTION 1: Initialization & Setup
dnl SECTION 2: Architecture, target, and host checks
dnl SECTION 3: Command line arguments for the configure script.
dnl SECTION 4: Check for programs we need and that they are the right version
dnl SECTION 5: Check for libraries
dnl SECTION 6: Check for header files
dnl SECTION 7: Check for types and structures
dnl SECTION 8: Check for specific functions needed
dnl SECTION 9: Additional checks, variables, etc.
dnl SECTION 10: Specify the output files and generate it
dnl
dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 1: Initialization & Setup
dnl===
dnl===-----------------------------------------------------------------------===
dnl Initialize autoconf and define the package name, version number and
dnl email address for reporting bugs.
AC_INIT([reactor],[0.1],[harris.bakiras@gmail.com])

dnl Provide a copyright substitution and ensure the copyright notice is included
dnl in the output of --version option of the generated configure script.
AC_SUBST(REACTOR_COPYRIGHT,["Copyright (c) 2011-2011 Purdue Univeristy/Université Pierre et Marie Curie."])
AC_COPYRIGHT([Copyright (c) 2011-2011 Purdue Univeristy/Université Pierre et Marie Curie.])

dnl Indicate that we require autoconf 2.59 or later. Ths is needed because we
dnl use some autoconf macros only available in 2.59.
AC_PREREQ(2.59)

dnl Verify that the source directory is valid. This makes sure that we are
dnl configuring reactor and not some other package (it validates --srcdir argument)
AC_CONFIG_SRCDIR([bootstrap.sh])

dnl Quit if the source directory has already been configured.
dnl NOTE: This relies upon undocumented autoconf behavior.
dnl if test ${srcdir} != "." ; then
dnl   if test -f ${srcdir}/include/vmkit/config.h ; then
dnl     AC_MSG_ERROR([Already configured in ${srcdir}])
dnl   fi
dnl fi

dnl Place all of the extra autoconf files into the config subdirectory. Tell
dnl various tools where the m4 autoconf macros are.
AC_CONFIG_AUX_DIR([./autoconf])


dnl Tell autoconf that this is an LLVM project being configured
dnl This provides the --with-llvmsrc and --with-llvmobj options
dnl LLVM_CONFIG_PROJECT($LLVM_SRC_ROOT,$LLVM_OBJ_ROOT)


dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 2: Architecture, target, and host checks
dnl===
dnl===-----------------------------------------------------------------------===

AC_CANONICAL_TARGET
dnl AM_INIT_AUTOMAKE


dnl Check the target for which we're compiling and the host that will do the
dnl compilations. This will tell us which LLVM compiler will be used for
dnl compiling SSA into object code. This needs to be done early because
dnl following tests depend on it.

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 3: Command line arguments for the configure script.
dnl===
dnl===-----------------------------------------------------------------------===

AC_ARG_WITH([vmkit-src],
	   [AS_HELP_STRING(--with-vmkit-src=src_dir_path,
           [VMKit source path])],
       	   [vmkitsrc=$withval],
       	   [AC_MSG_ERROR([[VMKit path not set, please use --with-vmkit-src option]])]
)

AC_CHECK_FILES([${vmkitsrc}]
               [${vmkitsrc}/Makefile.common], ,
	       AC_MSG_ERROR([[Invalid VMKit path]])
)

AC_ARG_WITH([vmkit-obj],
	   [AS_HELP_STRING(--with-vmkit-obj=obj_dir_path [default is vmkit-src path],
           [VMKit object path])],
       	   [vmkitobj=$withval],
       	   [vmkitobj=${vmkitsrc}])

AC_SUBST([vmkitsrc])
AC_SUBST([vmkitobj])


AC_ARG_WITH([antlr-jar],
	    [AS_HELP_STRING(--with-antlr-jar=jar_file_path, [Path to antlr.jar file])],
            [antlrjar=$withval],
            [antlrjar=""]
)

AC_ARG_WITH([antlr-c],
	    [AS_HELP_STRING(--with-antlr-c=antlrc_dir_path, [Antlr C library installation or compilation path])],
       	    [antlrc=$withval],
       	    [antlrc=""]
)

AC_ARG_ENABLE(debug,
   AS_HELP_STRING(--enable-debug,[Build with debug mode (default is NO)]),,enableval=no)

if test ${enableval} = "yes" ; then
  AC_DEFINE([R_DEBUG], [1], [debug mode])
fi

AC_ARG_ENABLE(jit-debug,
   AS_HELP_STRING(--enable-jit-debug,[Build with JIT debug mode (default is NO)]),,enableval=no)

if test ${enableval} = "yes" ; then
  AC_DEFINE([R_JIT_DEBUG], [1], [debug mode])
fi

AC_ARG_ENABLE(fast-lookup,
   AS_HELP_STRING(--enable-fast-lookup,[Use fast lookup for symbols]),,enableval=yes)

if test ${enableval} = "yes" ; then
  AC_DEFINE([R_FAST_LOOKUP], [1], [exact-keywords mode])
fi

AC_ARG_ENABLE(exact-keywords,
   AS_HELP_STRING(--enable-exact-keywords,[Only accept exact keywords for function call (default is NO)]),,enableval=no)

if test ${enableval} = "yes" ; then
  AC_DEFINE([R_EXACT_KEYWORDS], [1], [exact-keywords mode])
fi

AC_ARG_ENABLE(gctorture,
   AS_HELP_STRING(--enable-gctorture,[Build with GC torture mode (default is NO)]),,enableval=no)

if test ${enableval} = "yes" ; then
  AC_DEFINE([R_GCTORTURE], [1], [gctorture mode])
fi


dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 4: Check for programs we need and that they are the right version
dnl===
dnl===-----------------------------------------------------------------------===

dnl Check for compilation tools
AC_PROG_CPP


dnl Find the install program
AC_PROG_INSTALL

dnl Check for antlr tool
if test x"$antlrjar" = x; then
   AC_CHECK_TOOL([antlr], [antlr3])
	 if test x"$antlr" = x; then
      AC_MSG_ERROR([antlr tool not found: try --with-antlr-jar option])
   fi
else 
   AC_CHECK_FILE([${antlrjar}], , AC_MSG_ERROR([[Unable to find antlr in path: $antlrjar]]))
   antlr="java -jar $antlrjar"
fi

AC_SUBST(antlr)   

AC_SUBST(SHLIBEXT,$libltdl_cv_shlibext)

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 5: Check for libraries
dnl===
dnl===-----------------------------------------------------------------------===

AC_DEFUN([MY_CHECK_LIB],
[save_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS $3"
AC_CHECK_LIB([$1], [$2], [$4], [$5])
LDFLAGS="$save_LDFLAGS"
])

dnl Check for C runtime of antlr
if test x"$antlrc" = x; then
   AC_CHECK_LIB([antlr3c], [antlr3BaseRecognizerNew], [antlrcldflags="-lantlr3c"], 
		AC_MSG_ERROR([ANTLR3 C runtime (libantlr3c) not found:\
		try --with-antlr-c option])
   )
   AC_CHECK_HEADERS([antlr3config.h antlr3baserecognizer.h], [antlrccflags=""], 
      AC_MSG_ERROR([antlr3c header files not found])
   )
else
   AC_CHECK_FILES([${antlrc}/antlr3config.h ${antlrc}/include/antlr3baserecognizer.h], 
      [antlrccflags="-I${antlrc} -I${antlrc}/include"], 
      AC_MSG_ERROR([ANTLR3 C runtime headers not found in path: ${antlrc}])
   )

 	 MY_CHECK_LIB([antlr3c], [antlr3BaseRecognizerNew], [-L${antlrc}/.libs], 
	 												 [antlrcldflags="-L${antlrc}/.libs -Wl,-rpath,${antlrc}/.libs -lantlr3c"], 
 													 AC_MSG_ERROR([ANTLR3 C runtime (libantlr3c) not found: try --with-antlr-c option]))
fi

AC_SUBST([antlrccflags])
AC_SUBST([antlrcldflags])

if test "`uname -a | grep -i linux`" = "`uname -a`"; then
   LDFLAGS+=" -rdynamic"
fi

dnl Propagate the shared library extension that the libltdl checks did to
dnl the Makefiles so we can use it there too

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 6: Check for header files
dnl===
dnl===-----------------------------------------------------------------------===

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 7: Check for types and structures
dnl===
dnl===-----------------------------------------------------------------------===

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 8: Check for specific functions needed
dnl===
dnl===-----------------------------------------------------------------------===

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 9: Additional checks, variables, etc.
dnl===
dnl===-----------------------------------------------------------------------===

dnl===-----------------------------------------------------------------------===
dnl===
dnl=== SECTION 10: Specify the output files and generate it
dnl===
dnl===-----------------------------------------------------------------------===

dnl Configure header files
dnl WARNING: dnl If you add or remove any of the following config headers, then
dnl you MUST also update Makefile.rules so that the variable FilesToConfig
dnl contains the same list of files as AC_CONFIG_HEADERS below. This ensures the
dnl files can be updated automatically when their *.in sources change.
AC_CONFIG_HEADERS([lib/config.h])

AC_CONFIG_FILES([Makefile.config])

AC_OUTPUT
